import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import router from './router'
import Cookie from 'js-cookie'
import ECharts from "vue-echarts";
import 'echarts';
Vue.component('ECharts', ECharts) //全局注册



Vue.config.productionTip = false

Vue.use(ElementUI);
//注册一个全局前置守卫
router.beforeEach((to, from, next) => {
  //判断token是否存在
  const token = Cookie.get('token')
  if(!token && to.path != '/login' ){//当token为空 并且当前页为登录页
    next({ path : '/login'})
  } else if(token && (to.path === '/' || to.path === '/login')){ //当存在token时说明用户已登录 直接跳转到主页
    next({ path: '/admin'})
  }else{
    next()
  }
})

new Vue({
  router,
  render: h => h(App),
}).$mount('#app')
